﻿using System;
using System.Collections;
using System.Web.Mvc;
using AgileBoard.Domain;
using AgileBoard.Dtos;
using AgileBoard.Dtos.Mappers;
using AgileBoard.Web.Controllers.Util;

namespace AgileBoard.Web.Controllers
{
    public class TaskController : BaseController
    {
        // POST: /Task/Update/Json, Update, NewStatus

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Update(TaskDTO Json, Boolean Update, DefaultStatus? NewStatus)
        {
            var rdto = new ResultDTO();
            bool found = false;
            Label InitialLabel = ManagerFactory.LabelManager.NewLabel();
            Task task = TaskDTOMapper.MapFromDTO(Json);
            string IterationID = (task.Story != null) ? task.Story.IterationID : task.IterationID;

            try
            {
                if (!Update)
                {
                    task.TaskID = Guid.Empty;
                    task.Name = "New Task";
                    task.CreateTime = DateTime.Now;
                    task.CreateDate = DateTime.Now;
                    task.Evaluation = 1.0;
                    task.Remaining = 1.0;
                    InitialLabel.IterationID = task.IterationID;

                    //check whether task is being directly added to InProress status, if so update Start date to today date
                    if (Enum.IsDefined(typeof (DefaultStatus), task.Status.Name.Replace(" ", "")) &&
                        (DefaultStatus) Enum.Parse(typeof (DefaultStatus), task.Status.Name.Replace(" ", "")) ==
                        DefaultStatus.InProgress)
                    {
                        task.StartDate = DateTime.Now;
                    }

                    IList Labels = ManagerFactory.LabelManager.GetByIteration(IterationID);
                    foreach (Label label in Labels)
                    {
                        if (label.Date.Trim() == DateTime.Now.ToShortDateString() ||
                            DateTime.Compare(Convert.ToDateTime(label.Date), DateTime.Now) > 0)
                        {
                            InitialLabel = label;
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        //InitialLabel = ManagerFactory.LabelManager.Save(InitialLabel);
                    }

                    Actual newActual = ManagerFactory.ActualsManager.NewActual();
                    newActual.TaskID = task.TaskID.ToString();
                    newActual.Label = InitialLabel;
                    newActual.Remain = task.Remaining;
                    newActual = ManagerFactory.ActualsManager.Save(newActual);
                    task.AddActual(newActual);
                }

                if (NewStatus == DefaultStatus.InProgress)
                {
                    task.StartDate = DateTime.Now;
                }
                else if (NewStatus == DefaultStatus.Done)
                {
                    task.FinishDate = DateTime.Now;
                    task.Remaining = 0.0;
                }

                foreach (Actual actual in task.Actuals)
                {
                    if (actual.Label == null) continue;
                    if (actual.Label.Date.Trim() == DateTime.Now.ToShortDateString())
                    {
                        actual.Remain = task.Remaining;
                    }
                }

                task = ManagerFactory.TaskManager.Update(task);
                rdto.Result = true;
                rdto.Message = "Success";
                rdto.Data = TaskDTOMapper.MapToDTO(task).ToExtJsJson();
            }
            catch (Exception e)
            {
                rdto.Message = e.Message;
                rdto.Result = false;
            }

            return this.Json(rdto);
        }

        // POST: /Task/Delete/GUID

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Delete(Guid Guid)
        {
            var rdto = new ResultDTO();
            try
            {
                ManagerFactory.TaskManager.Delete(Guid);
                rdto.Message = "Success";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = ex.Message;
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}